<?php

/**
 *
 */
Class Lib_CallCenter_ZhonTou_Curl
{

    public static $json_header = ['Content-Type：application/x-www-form-urlencoded; charset=utf-8;Accept: application/json;'];


    /**
     * @param $url
     * @param $params
     * @return mixed
     * get请求
     */
    public static function curlGet($url, $params = [], $timeout = 20,   $header = [],$nocheck = 0)
    {
        $logs = ' ssid=' . $params['ssid'];
        unset($params['ssid']);
        $url = strstr($url, '?') ? $url . '&' . urldecode(http_build_query($params)) : $url . '?' . urldecode(http_build_query($params));
        $logs .= ' request_url=' . $url;
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);


        $res = curl_exec($ch);
        $log['errno'] = curl_errno($ch);
        $log['httpcode'] = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        if ($log['errno'] !== 0) {
            curl_close($ch);
            Func_Log::write('callCenter_curl_ZhonTou', 'curl_response401', $logs . json_encode($log));
            $result['errno'] = $log['errno'];
            return $result;
        }
        curl_close($ch);
        if($nocheck){
            return $res;
        }
        $logs .= ' res =' . $res;
        $res = json_decode($res, true);

        if (!is_array($res) || count($res) <= 0) {
            //返回值错误日志
            $log['errno'] = 401;
            $log['message'] = 'Inlegal json response';
            Func_Log::write('callCenter_curl_ZhonTou', 'curl_response401', $logs . json_encode($log));
            $result['errno'] = $log['errno'];
            return $result;
        }
        Func_Log::write('callCenter_curl_ZhonTou', 'curl_end', $logs);
        $res['errno'] = 200;
        return $res;
    }

    public static  function curlPost($url, $params = [], $timeout = 20, $header = []){
        $logs = ' ssid=' . $params['ssid'];
        unset($params['ssid']);
        $logs .= ' request_url=' . $url;
        $logs .= ' $params=' . $params;
        $header = empty($header) ? self::$json_header : $header;
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_NOBODY, true);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
        curl_setopt($ch, CURLOPT_HTTPHEADER,$header );
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
        $res = curl_exec($ch);

        curl_close($ch);
        $logs .= ' res =' . $res;
        $res = json_decode($res, true);


        if (!is_array($res) || count($res) <= 0) {
            //返回值错误日志
            $log['errno'] = 401;
            $log['message'] = 'Inlegal json response';
            Func_Log::write('callCenter_curl_ZhonTou', 'curl_response401', $logs . json_encode($log));
            $result['errno'] = $log['errno'];
            return $result;
        }
        Func_Log::write('callCenter_curl_ZhonTou', 'curl_end', $logs);
        $res['errno'] = 200;
        return $res;
    }
}